package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ErpInventoryTransfer;
import com.ruoyi.service.IErpInventoryTransferService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 调拨管理Controller
 * 
 * @author zhp
 * @date 2025-08-08
 */
@RestController
@RequestMapping("/storage/allot")
public class ErpInventoryTransferController extends BaseController
{
    @Autowired
    private IErpInventoryTransferService erpInventoryTransferService;

    /**
     * 查询调拨管理列表
     */
    @PreAuthorize("@ss.hasPermi('storage:allot:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpInventoryTransfer erpInventoryTransfer)
    {
        startPage();
        List<ErpInventoryTransfer> list = erpInventoryTransferService.selectErpInventoryTransferList(erpInventoryTransfer);
        return getDataTable(list);
    }

    /**
     * 导出调拨管理列表
     */
    @PreAuthorize("@ss.hasPermi('storage:allot:export')")
    @Log(title = "调拨管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpInventoryTransfer erpInventoryTransfer)
    {
        List<ErpInventoryTransfer> list = erpInventoryTransferService.selectErpInventoryTransferList(erpInventoryTransfer);
        ExcelUtil<ErpInventoryTransfer> util = new ExcelUtil<ErpInventoryTransfer>(ErpInventoryTransfer.class);
        util.exportExcel(response, list, "调拨管理数据");
    }

    /**
     * 获取调拨管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('storage:allot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(erpInventoryTransferService.selectErpInventoryTransferAndDetailById(id));
    }

    /**
     * 修改调拨管理
     */
//    @PreAuthorize("@ss.hasPermi('storage:allot:edit')")
//    @Log(title = "调拨管理", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody ErpInventoryTransfer erpInventoryTransfer)
//    {
//        return toAjax(erpInventoryTransferService.updateErpInventoryTransfer(erpInventoryTransfer));
//    }

    /**
     * 删除调拨管理
     */
    @PreAuthorize("@ss.hasPermi('storage:allot:remove')")
    @Log(title = "调拨管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(erpInventoryTransferService.deleteErpInventoryTransferByIds(ids));
    }


    //调拨申请
    @PostMapping("/addAllot")
    @PreAuthorize("@ss.hasPermi('storage:allot:add')")
    public AjaxResult allocationApplication(@RequestBody ErpInventoryTransfer erpInventoryTransfer) {
//        设置调拨主表状态
        erpInventoryTransfer.setStatus(0L);
//        设置调拨主表申请人id

        erpInventoryTransfer.setOperatorId(SecurityUtils.getLoginUser().getUser().getUserId());
        try{
            erpInventoryTransferService.insertErpInventoryTransfer(erpInventoryTransfer);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error("调拨单提交失败："+e.getMessage());
        }
    }


    /**
     * 审核调拨通过
     */
    @PreAuthorize("@ss.hasPermi('storage:allot:approveAllot')")
    @PutMapping("/approveAllot")
    public AjaxResult approveAllot(@RequestBody ErpInventoryTransfer erpInventoryTransfer) {
        return toAjax(erpInventoryTransferService.approveAllot(erpInventoryTransfer));

    }


    /**
     * 审核调拨拒绝
     */
    @PreAuthorize("@ss.hasPermi('storage:allot:refuseAllot')")
    @PutMapping("/refuseAllot")
    public AjaxResult refuseAllot(@RequestBody ErpInventoryTransfer erpInventoryTransfer) {
        return toAjax(erpInventoryTransferService.refuseAllot(erpInventoryTransfer));

    }




}
